/*
 * =====================================================================================
 *
 *       Filename:  test.cpp
 *
 *    Description:  Test bivariate normal distribution
 *
 *        Version:  1.0
 *        Created:  25/07/12 14:51:41
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <math.h>

#include "bivgauss.h"

using namespace std;
using namespace cv;

Mat bivGauss(int s, double mux, double muy, double sx, double sy, double r, double weight) {
    Mat data(s, s, CV_64FC1);

    double qxy;
    double c = 1./(2*M_PI*sqrt(1 - r*r)*sx*sy);

    for (int x = -(s-1)/2; x <= (s-1)/2; x++) {
        for (int y = -(s-1)/2; y <= (s-1)/2; y++) {
            qxy = (((x - mux)*(x - mux)/(sx*sx) - 2*r*((x-mux)*(y-muy))/(sx*sy)) + ((y-muy)*(y-muy))/(sy*sy) ) / (2*(1 - r*r));
//            qxy = (((x*x)/(sx*sx) - 2*r*(x*y)/(sx*sy)) + (y*y)/(sy*sy)) / (2*(1 - r*r));
            data.at<double>(x+((s-1)/2),y+((s-1)/2))=c*exp(-1*qxy);
        }
    }

    double max;
    minMaxLoc(data, NULL, &max);

    data = weight*data;

    if (max > 0)
        data /= max;
    
    return data;
}

//int main(int argc, char* argv[]) {
//    int s = atof(argv[1]);
//    double sx, sy;
//    sx = atof(argv[2]);
//    sy = atof(argv[3]);
//    double r = atof(argv[4]);
//    double mux = 0.;
//    double muy = 0.;
//
//    Mat data = bivGauss(s, mux, muy, sx, sy, r, 0.5);
//    resize(data,data,Size(0,0),20,20,INTER_NEAREST);
////    imwrite("bivgauss0.png", data);
//
//}

